package com.bms.controller;

import com.alibaba.fastjson.JSON;
import com.bms.Dao.BookDao;
import com.bms.Dao.LentDao;
import com.bms.Dao.UserBookDao;
import com.bms.Dao.UserDao;
import com.bms.bean.Book;
import com.bms.bean.lentInfo;
import com.bms.bean.userbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LentInfoController {
    @Autowired
    LentDao lentDao;

    @Autowired
    UserBookDao userBookDao;

    @Autowired
    BookDao bookDao;

//    通过读者证号和图书id借书
    @RequestMapping("/lentBook")
    public String lentBook(@RequestBody lentInfo lentInfo)
    {
        lentDao.lentBook(lentInfo.getBook_id(),lentInfo.getReader_id(),lentInfo.getLenttime(),lentInfo.getS_backtime(),0);
        bookDao.subBook(lentInfo.getBook_id());
        return "ok";
    }

//    通过图书id和读者证编号还书
    @RequestMapping("/backBook")
    public String bockBook(@RequestBody lentInfo lentInfo)
    {
        //lentInfo lentInfo=new lentInfo();
        //lentInfo.setBook_id(1);
        lentDao.backBook(lentInfo.getBook_id(),lentInfo.getReader_id(),lentInfo.getBacktime());
        bookDao.addBook(lentInfo.getBook_id(),1);
        Book book=bookDao.getBookById(lentInfo.getBook_id());
        return JSON.toJSONString(book);
    }

//    查询用户未还图书
    @RequestMapping("/noBack")
    public String noBack(@RequestHeader("X-Token") String id)
    {
        userbook[] books=userBookDao.queryBook(Integer.parseInt(id));
        return JSON.toJSONString(books);
    }

//    查询用户已还图书
    @RequestMapping("/isBack")
    public String isBack(@RequestHeader("X-Token") String id)
    {
         userbook[] books=userBookDao.queryBackBook(Integer.parseInt(id));
         return JSON.toJSONString(books);
    }

}

